<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>号码抽奖</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
            background-color: #f4f7f6;
            color: #333;
            margin: 0;
            padding: 20px;
        }

        .container {
            background-color: #fff;
            padding: 30px 40px;
            border-radius: 12px;
            box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
            text-align: center;
            width: 100%;
            max-width: 600px;
        }

        h1 {
            color: #007bff;
            margin-bottom: 25px;
            font-size: 2.5em;
        }

        p {
            font-size: 1.1em;
            line-height: 1.6;
        }

        #currentTurn {
            font-size: 1.3em;
            color: #28a745;
            font-weight: bold;
            margin-bottom: 20px;
        }

        #drawButton {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 15px 30px;
            font-size: 1.4em;
            border-radius: 8px;
            cursor: pointer;
            transition: background-color 0.3s ease, transform 0.2s ease;
            margin-top: 20px;
            outline: none;
        }

        #drawButton:hover {
            background-color: #0056b3;
            transform: translateY(-2px);
        }

        #drawButton:disabled {
            background-color: #cccccc;
            cursor: not-allowed;
            transform: none;
        }

        #result {
            margin-top: 30px;
            font-size: 1.2em;
            color: #333;
            background-color: #e9ecef;
            padding: 15px;
            border-radius: 8px;
            border: 1px solid #dee2e6;
            min-height: 50px;
        }

        #pastResults {
            margin-top: 40px;
            text-align: left;
            width: 100%;
            max-width: 600px;
        }

        #pastResults h2 {
            color: #6c757d;
            border-bottom: 2px solid #007bff;
            padding-bottom: 10px;
            margin-bottom: 20px;
            font-size: 1.8em;
        }

        .player-result {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 10px 0;
            border-bottom: 1px dashed #ced4da;
        }

        .player-result:last-child {
            border-bottom: none;
        }

        .player-result strong {
            color: #007bff;
            font-size: 1.2em;
        }

        .player-result span {
            font-size: 1.2em;
            color: #28a745;
            font-weight: bold;
        }

        .final-message {
            font-size: 1.5em;
            color: #dc3545;
            font-weight: bold;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>号码抽奖</h1>
        <p>总共有 40 个号码（1 到 40），5 位选手轮流抽取。</p>
        <p id="currentTurn"></p>
        <button id="drawButton">抽取号码</button>
        <div id="result">点击 "抽取号码" 开始抽奖！</div>
    </div>

    <div id="pastResults" class="container">
        <h2>抽奖结果记录</h2>
        <div id="resultsList">
            <!-- 抽奖结果会在这里显示 -->
            <p style="text-align: center; color: #6c757d;">暂无抽奖记录</p>
        </div>
    </div>

    <script>
        const players = ["James", "kris", "zero", "chris", "ivy"];
        let availableNumbers = Array.from({ length: 40 }, (_, i) => i + 1); // 1到40
        let currentPlayerIndex = 0;
        let round = 1; // 记录第几轮抽奖
        let drawCount = 0; // 记录总共抽了多少次
        const maxDraws = availableNumbers.length; // 最多可以抽40次

        const currentTurnElement = document.getElementById("currentTurn");
        const drawButton = document.getElementById("drawButton");
        const resultElement = document.getElementById("result");
        const resultsListElement = document.getElementById("resultsList");

        function updateTurnDisplay() {
            if (drawCount < maxDraws) {
                currentTurnElement.textContent = `当前轮到：${players[currentPlayerIndex]} 抽号 (第 ${round} 轮)`;
            } else {
                currentTurnElement.textContent = "所有号码已抽完！";
                drawButton.disabled = true;
                drawButton.textContent = "抽奖结束";
                resultElement.textContent = "所有号码已抽完，抽奖结束！";
                resultElement.classList.add("final-message");
            }
        }

        function drawNumber() {
            if (availableNumbers.length === 0) {
                resultElement.textContent = "所有号码已抽完，抽奖结束！";
                drawButton.disabled = true;
                drawButton.textContent = "抽奖结束";
                return;
            }

            const randomIndex = Math.floor(Math.random() * availableNumbers.length);
            const drawnNumber = availableNumbers.splice(randomIndex, 1)[0]; // 移除并获取抽中的号码

            const currentPlayer = players[currentPlayerIndex];
            resultElement.innerHTML = `恭喜 <strong>${currentPlayer}</strong> 抽中了号码：<span style="color: #dc3545; font-size: 1.5em;">${drawnNumber}</span>`;

            // 更新抽奖结果记录
            if (resultsListElement.querySelector('p')) { // 移除“暂无记录”提示
                resultsListElement.querySelector('p').remove();
            }
            const playerResultDiv = document.createElement("div");
            playerResultDiv.classList.add("player-result");
            playerResultDiv.innerHTML = `<strong>${currentPlayer}</strong> 抽中: <span>${drawnNumber}</span>`;
            resultsListElement.prepend(playerResultDiv); // 新的结果显示在最上面

            // 更新下一位抽奖者
            currentPlayerIndex = (currentPlayerIndex + 1) % players.length;
            drawCount++;

            // 如果所有玩家都轮了一圈，增加轮次
            if (currentPlayerIndex === 0 && drawCount < maxDraws) {
                round++;
            }

            updateTurnDisplay(); // 更新显示
        }

        // 初始化显示
        updateTurnDisplay();
        drawButton.addEventListener("click", drawNumber);
    </script>
</body>
</html>